from django.db import models
from simcat.uuid_field import UuidField
from simcat.models import Member
from simcat.models import Computer, Queue
from simcat.models import Program, Parameter
from simcat.models import Simulation
from simcat.models import CharacterizableType, Property

CHARACTERIZATION_CHOICES = (('lb', 'Lower_bound'),
                            ('ub', 'Upper bound'),
                            ('min', 'Minimum'),
                            ('max', 'Maximum'),
                            ('mean', 'Mean'),
                            ('std_dev', 'Standard deviation'))

class Run(models.Model):
    uuid = UuidField(primary_key=True, auto=True)
    name = models.CharField(max_length=128)
    member = models.ForeignKey(Member)
    simulation = models.ForeignKey(Simulation, null=True)
    description = models.TextField(null=True, blank=True)
    program = models.ForeignKey(Program)
    version = models.CharField(max_length=512, null=True, blank=True)
    computer = models.ForeignKey(Computer, null=True, blank=True)
    queue = models.ForeignKey(Queue, null=True, blank=True)
    wall_clock_limit = models.TimeField("Wall clock time requested", null=True, blank=True)
    submittime = models.DateTimeField("Submitted time", null=True, blank=True)
    starttime = models.DateTimeField("Start time", null=True, blank=True)
    endtime = models.DateTimeField("End time", null=True, blank=True)
    comments = models.TextField(null=True, blank=True)
    todo = models.TextField("ToDo", null=True, blank=True)
    successful = models.NullBooleanField(blank=True)
    jobid = models.CharField("JobID", max_length=128, null=True, blank=True)
    jobname = models.CharField("JobName", max_length=128, null=True, blank=True)
    account = models.CharField(max_length=128, null=True, blank=True)
    nodes = models.PositiveIntegerField("Number of nodes", null=True, blank=True)
    tasks_per_node = models.PositiveIntegerField("Tasks per node", null=True, blank=True)

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

    def __unicode__(self):
        return "%s: %s" % (self.name, self.program.name)

class ParameterSetting(models.Model):
    run = models.ForeignKey(Run)
    parameter = models.ForeignKey(Parameter)
    value = models.CharField(max_length=512)

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

    def __unicode__(self):
        return "%s: %s" % (self.parameter.name, self.value)

class UserField(models.Model):
    run = models.ForeignKey(Run)
    name = models.SlugField()
    value = models.CharField(max_length=512)

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

    def __unicode__(self):
        return "%s: %s" % (self.parameter.name, self.value)


class Dataset(models.Model):
    uuid = UuidField(primary_key=True, auto=True)
    run = models.ForeignKey(Run)
    simulation_time = models.FloatField(null=True)
    physical_time = models.FloatField(null=True)
    url = models.URLField(null=True, blank=True)
    mime_type = models.SlugField(null=True, blank=True)
    size = models.PositiveIntegerField(null=True, blank=True)

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

# stupid hack to create linking table
class InputDataset(models.Model):
    run = models.ForeignKey(Run)
    dataset = models.ForeignKey(Dataset)

    class Admin:
        pass

    class Meta:
        unique_together = ("run", "dataset")
	app_label = "simcat"

class Characterization(models.Model):
    property = models.ForeignKey(Property)
    dataset = models.ForeignKey(Dataset)
    quantity = models.CharField(max_length = 2, 
                                choices=CHARACTERIZATION_CHOICES)
    value = models.FloatField()
    units = models.CharField(max_length=128, null=True, blank=True)
    ucd = models.SlugField(null=True, blank=True)

    class Meta:
	app_label = "simcat"

class ObjectCollection(models.Model):
    dataset = models.ForeignKey(Dataset)
    representation = models.ForeignKey(CharacterizableType)
    count = models.PositiveIntegerField()

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

